﻿Imports myBookmarks.Core
Namespace myBookmarks.Web
    Public Module HtmlHelperExtensions

        <System.Runtime.CompilerServices.Extension()> _
        Public Function LastUpdated(ByVal helper As HtmlHelper, ByVal lastupdate As Date) As String
            Dim dateText As String = "<div class=""month"">{0}</div><div class=""day"">{1}</div><div class=""year"">{2}</div>"
            Return String.Format(dateText, lastupdate.ToString("MMM"), lastupdate.Day, lastupdate.Year)
        End Function
        <System.Runtime.CompilerServices.Extension()> _
        Public Function SiteName(ByVal helper As HtmlHelper, ByVal location As String) As String
            Dim urlName As UriBuilder = New UriBuilder(location)
            Return urlName.Host
        End Function
        <System.Runtime.CompilerServices.Extension()> _
        Public Function LabelNames(ByVal helper As HtmlHelper, ByVal labels As List(Of Label)) As String
            Dim result As String = String.Empty
            For Each l As Label In labels
                result = result & String.Format("{0},", l.LabelName)
            Next
            Return result.TrimEnd(Convert.ToChar(","))
        End Function

        <System.Runtime.CompilerServices.Extension()> _
            Public Function CustomValidationSummary(ByVal html As HtmlHelper) As String
            If Not html.ViewData.ModelState.IsValid Then
                If html.ViewData.ModelState.ContainsKey("_Form") Then
                    Return "<div class=""display-message"">" & html.ViewData.ModelState("_Form").Errors(0).ErrorMessage & "</div>"
                End If
            End If
            Return ""
        End Function
        <System.Runtime.CompilerServices.Extension()> _
        Public Function ConvertToUnixTimestamp(ByVal helper As HtmlHelper, ByVal [date] As DateTime) As Double
            Dim origin As New DateTime(1970, 1, 1, 0, 0, 0, _
            0)
            Dim diff As TimeSpan = [date] - origin
            Return Math.Floor(diff.TotalSeconds)
        End Function
        <System.Runtime.CompilerServices.Extension()> _
        Public Function GetRootPath(ByVal helper As HtmlHelper) As String
            Dim urlHelper = New UrlHelper(helper.ViewContext.RequestContext)
            Return urlHelper.Content("~/")
        End Function
        <System.Runtime.CompilerServices.Extension()> _
        Public Function CreateRouteToCurrentPage(ByVal helper As HtmlHelper) As RouteValueDictionary
            Dim routeValues As New RouteValueDictionary(helper.ViewContext.RouteData.Values)
            Dim queryString As NameValueCollection = helper.ViewContext.HttpContext.Request.QueryString

            For Each key As String In queryString.Cast(Of String)()
                routeValues(key) = queryString(key)
            Next
            Return routeValues
        End Function
        <System.Runtime.CompilerServices.Extension()> _
        Public Function CreateSortLinks(ByVal helper As HtmlHelper, ByVal sort As String) As RouteValueDictionary
            Dim returnroute As New RouteValueDictionary
            returnroute = CreateRouteToCurrentPage(helper)
            returnroute.Remove("page")
            returnroute("sort") = sort
            Return returnroute
        End Function
    End Module

End Namespace